<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属渠道" prop="tenantId">
        <el-select
              v-model="queryParams.tenantId"
              placeholder="请选择所属渠道"
              clearable
              :style="{ width: '100%' }"
            >
              <el-option
                v-for="(item, index) in tenantIdOptions"
                :key="index"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index" />
      <el-table-column label="SN码" align="center" prop="code" />
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getUnusedSNPage as getTenantPage,
} from "@/api/sn/index";
import { getTenantList } from "@/api/org/channel";
import { listSimpleMenus } from "@/api/system/menu";

export default {
  props: ['code'],
  name: "UsedPackage",
  components: {},
  data() {
    return {
      activeName: "bind",
      // 日期范围
      dateRange: [],
      testTypeDictData: [
        { label: "未发货", value: "to_deliver" },
        { label: "已发货", value: "delivered" },
      ],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租户套餐列表
      list: [],
      // 弹出层标题
      title: "",
      titleSN: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        tenantName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      formData: {},
      menuExpand: false,
      menuNodeAll: false,
      menuCheckStrictly: true,
      defaultProps: {
        label: "name",
        children: "children",
      },
      menuOptions: [], // 菜单列表
      // 表单校验
      
      
      payTypeOptions: [
        {
          label: "货到付款",
          value: "delivery",
        },
        {
          label: "月结",
          value: "monthly",
        },
      ],
      projectIdOptions: [],
      tenantIdOptions: [],
      discountPrice: "",
      //SN
      openSN: false,
      snBatchCodeOptions: [],
    };
  },
  created() {
    this.getList();
    this.getMenus();
    // this.getProject();
    this.getTenant();
  },
  watch: {
    code(newV, oldV){
      if(newV){
        this.getList()
      }
    }
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
    },
    changeBatch() {
      this.formData.snCodes = "";
      this.formData.expireTime = undefined
      this.setExpireTime();
    },
    //根据产品批次获取对应的过期时间
    setExpireTime() {
      const temp = this.snBatchCodeOptions.filter(
        (item) => item.code == this.formData.snBatchCode
      );
      const time = temp.length && temp[0].expireTime;
      if (time) {
        this.formData.expireTime = this.parseTime(time);
      }
    },
    getSNList(code) {
      getSN({ orderId: code }).then((response) => {
        this.snBatchCodeOptions = response.data || [];
      });
    },
    changeDiscount() {
      this.form.price = this.discountPrice;
    },
    changeProject() {
      getProjectInfo(this.form.projectId).then((response) => {
        const { price, discountPrice } = response.data;
        this.form.price = price;
        this.discountPrice = discountPrice;
      });
    },
    getTenant() {
      getTenantList({
        status: 0,
      }).then((response) => {
        this.tenantIdOptions = response.data || [];
      });
    },
    getProject() {
      getProjectList().then((response) => {
        this.projectIdOptions = response.data || [];
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = { ...this.queryParams, code: this.code };
      // this.addDateRange(this.queryParams, [
      //   this.dateRange[0] ? this.dateRange[0] + ' 00:00:00' : undefined,
      //   this.dateRange[1] ? this.dateRange[1] + ' 23:59:59' : undefined,
      // ])
      // 执行查询
      getTenantPage(params).then((response) => {
        this.list = response.data.list || [];
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    close() {
      this.openSN = false;
      this.resetSN();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      // 表单重置
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看样本 */
    checkSpecimen(row) {
      const { tenantName } = row
      this.$router.push({path: '/specimen', query: { tenantName: tenantName}})
    },
    /** 获得菜单 */
    getMenus() {
      listSimpleMenus().then((response) => {
        // 处理 menuOptions 参数
        this.menuOptions = [];
        // 只需要配置
        this.menuOptions.push(...this.handleTree(response.data, "id"));
      });
    },
    // 树权限（展开/折叠）
    handleCheckedTreeExpand(value, type) {
      let treeList = this.menuOptions;
      for (let i = 0; i < treeList.length; i++) {
        this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
      }
    },
    // 树权限（全选/全不选）
    handleCheckedTreeNodeAll(value) {
      this.$refs.menu.setCheckedNodes(value ? this.menuOptions : []);
    },
    // 树权限（父子联动）
    handleCheckedTreeConnect(value) {
      this.form.menuCheckStrictly = value;
    },
  },
};
</script>
<style scoped>
.discount {
  position: relative;
}
.discount .el-button {
  position: absolute;
  top: 4px;
  right: -16px;
}
</style>
